#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "gpio.h"
#include "interrupt.h"
#include "sysctl.h"
#include "uart.h"
#include "osram96x16x1.h"
#include "menu.h"
#include "driver_prog.h"



//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
// Global variables
//*****************************************************************************
struct LB led_and_button = {0};
struct PWM_parameters PWM[3] = {{0},{0},{0}}; 
struct ADC_parameters ADC[4] = {{0},{0},{0},{0}};
//*****************************************************************************
// Function for displaying on the OSRAM what was configured.
//*****************************************************************************
void DrawResult(void)
{
	OSRAM96x16x1Init(false);

	OSRAM96x16x1StringDraw("SET:", 0, 0);

	if( led_and_button.led_and_button[0] == 1 )
		OSRAM96x16x1StringDraw("LED", 70, 0);
			
	if( led_and_button.led_and_button[1] == 1 )
		OSRAM96x16x1StringDraw("Button", 54, 8);

	if( (PWM[0].check | PWM[1].check | PWM[2].check) == 1 )
	{
		OSRAM96x16x1StringDraw("PWM", 24, 0);
		if( PWM[0].check == 1 )
			OSRAM96x16x1StringDraw("0", 44, 0);
		if( PWM[1].check == 1 )
			OSRAM96x16x1StringDraw("1", 52, 0);
		if( PWM[2].check == 1 )
			OSRAM96x16x1StringDraw("2", 60, 0);	
	}

	if( (ADC[0].check | ADC[1].check | ADC[2].check | ADC[3].check) == 1 )
	{	
		OSRAM96x16x1StringDraw("ADC", 0, 8);
		if( ADC[0].check == 1 )
			OSRAM96x16x1StringDraw("0", 20, 8);
		if( ADC[1].check == 1 )
			OSRAM96x16x1StringDraw("1", 28, 8);
		if( ADC[2].check == 1 )
			OSRAM96x16x1StringDraw("2", 36, 8);
		if( ADC[3].check == 1 )
			OSRAM96x16x1StringDraw("3", 44, 8);	
	}
}
//*****************************************************************************
// The main function in the program.
//Setting the clocking to run directly from the crystal. 
//Enabling the peripherals used by the program (UART0).
//Enabling processor interrupts. Configuring the UART for 115,200, 8-N-1 operation. 
//Sending via the UART the menu to the terminal. Reading the choice of the user. 
//Drawing the result on the OSRAM display by using DrawResult(). 
//Activating the configured devices form their driver functions.
//*****************************************************************************
int
main(void)
{

	//
    // Set the clocking to run directly from the crystal.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);
    //
    // Enable the peripherals used by this example.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    //
    // Set GPIO A0 and A1 as UART pins.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

   //
   //Start menu
   //
	while(1){
		UARTSend((unsigned char *)"\n\rEnter device to configure:\n\r"
									"1. ADC\n\r"
									"2. PWM\n\r"
									"3. LED\n\r"
									"4. Button\n\r",65);
		switch(ChoiseNum(4))
		{
			case 1: AdcSet(ADC); break;
			case 2: PwmSet(PWM); break;
			case 3: led_and_button = led(led_and_button); break;
			case 4: led_and_button = button(led_and_button);break;
			default: break;
		}
		UARTSend((unsigned char *)"\n\rDo you want to configure another device?\n\r"
									"1. YES\n\r"
									"2. NO\n\r",59);
		if( ChoiseNum(2) == 1 )
		continue;
		else break;
	 
	}
	UARTSend((unsigned char *)"\n\rYou have configured the devices!\n\r",36);
	//
	//Configure the choisen divices
	//

	LedAndButtonDriver(led_and_button);
	PWM_driver( PWM );
	ADC_driver(ADC);
	//
    //Displaying on the OSRAM what was configured. 
	//
    IntMasterDisable();
	DrawResult();
    IntMasterEnable();
 


	while(1);
}


